import { get } from '../api/getData';
// 分享
function Share(config) {
    window.wx.config({
        debug: config.debug, // 开启调试模式
        appId: config.appid, // 必填，公众号的唯一标识
        timestamp: config.timestamp, // 必填，生成签名的时间戳
        nonceStr: config.nonceStr, // 必填，生成签名的随机串
        signature: config.signature, // 必填，签名，见附录1
        jsApiList: ['checkJsApi','onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareWeibo'], // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
    });
}

Share.prototype = {
    constructor: Share,

    init(config) {
        this.imgUrl = config.imgUrl;
        this.link = config.link;
        this.description = config.description;
        this.title = config.title;

        window.wx.ready(() => {
            this.checkJsApi();
            this.toFriend();
            this.toTimeline();
        });

        window.wx.error(res => {
            console.log(`${res}`);
        });
    },

    checkJsApi(){
        window.wx.checkJsApi({
            jsApiList: [
                'updateAppMessageShareData',
                'updateTimelineShareData'
            ],
            success: function (res) {
                console.log(JSON.stringify(res));
            },
            error : function(res){
                console.log(JSON.stringify(res));
            }
        });
    },

    toFriend() {
        window.wx.onMenuShareAppMessage({
            imgUrl: this.imgUrl,
            link: this.link,
            title: this.title,
            desc: this.description,
            success: function () {
                // get('/api/share/tofriend', {shareLink:this.link}, function (error, data) {
                //     console.log(data);
                // });
                // 用户确认分享后执行的回调函数
            },
        });
    },

    toTimeline() {
        window.wx.onMenuShareTimeline({
            imgUrl: this.imgUrl,
            link: this.link,
            title: this.title,
            desc: this.description,
            success: function () {
                // get('/api/share/totimeline', {shareLink:this.link}, function (error, data) {
                //     console.log(data);
                // });
            },
        });
    },
};

export default Share;
